package xyz.playedu.api.bus;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.playedu.api.request.backend.CourseChapterHourRequest;
import xyz.playedu.common.constant.BackendConstant;
import xyz.playedu.common.types.JsonResponse;
import xyz.playedu.course.domain.CourseChapter;
import xyz.playedu.course.domain.CourseHour;
import xyz.playedu.course.service.CourseChapterService;
import xyz.playedu.course.service.CourseHourService;
import xyz.playedu.course.service.CourseService;
import xyz.playedu.resource.service.ResourceService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenguiying
 * @since 2024/7/25 9:01
 */
@Component
@Slf4j
public class CourseHourBus {

    @Autowired private CourseHourService hourService;

    @Autowired private ResourceService resourceService;

    @Autowired private CourseService courseService;

    @Autowired private CourseChapterService chapterService;

    public JsonResponse updateBatch (Integer courseId, CourseChapterHourRequest req) {
        List<CourseHour> updateHours = new ArrayList<>();
        List<CourseHour> addHour = new ArrayList<>();

//        todo 前端修改update提交的参数类型，后端分两种情况进行判断
        if (ObjectUtil.isNotEmpty(req.getHours())) {
            buildParam(req.getHours(), addHour, updateHours, courseId);
        }
        for (CourseChapterHourRequest.chapterHourItem chapterHour : req.getChapterHours()) {
            if (ObjectUtil.isEmpty(chapterHour.getId())) {
                CourseChapter courseChapter = chapterService.create(courseId, chapterHour.getName(), chapterHour.getSort());
                chapterHour.getHours().stream().forEach(e -> e.setChapterId(courseChapter.getCourseId()));
            } else {
                CourseChapter courseChapter = new CourseChapter() {{
                    setId(chapterHour.getId());
                    setName(chapterHour.getName());
                }};
                chapterService.updateById(courseChapter);
            }
            buildParam(chapterHour.getHours(), addHour, updateHours, courseId);
        }
        if (ObjectUtil.isNotEmpty(addHour)) {
            hourService.saveBatch(addHour);
        }
        if (ObjectUtil.isNotEmpty(updateHours)) {
            hourService.updateBatchById(updateHours);
        }
        // 修改课时数
        List<CourseHour> hoursByCourseId = hourService.getHoursByCourseId(courseId);
        courseService.updateClassHour(courseId, hoursByCourseId.size());

        return JsonResponse.success();
    }

    private void buildParam(List<CourseChapterHourRequest.HourItem> hours
            , List<CourseHour> addHour,  List<CourseHour> updateHours, Integer courseId) {
        Date now = new Date();
        List<Integer> existsRids =
                hourService.getRidsByCourseId(courseId, null);
        for (CourseChapterHourRequest.HourItem hour : hours) {
            if (ObjectUtil.isEmpty(hour.getId())) {
                if (existsRids.contains(hour.getRid())) {
                    return ;
                }
                if(BackendConstant.RESOURCE_TYPE_VIDEO.equals(hour.getType())) {
                    hour.setDuration(resourceService.duration(hour.getRid()));
                }
                addHour.add(
                        new CourseHour() {
                            {
                                setCourseId(courseId);
                                setChapterId(hour.getChapterId());
                                setSort(hour.getSort());
                                setType(hour.getType());
                                setRid(hour.getRid());
                                setTitle(hour.getTitle());
                                setDuration(hour.getDuration());
                                setCreatedAt(now);
                            }
                        });
            } else {
                if (BackendConstant.RESOURCE_TYPE_PDF.equals(hour.getType())) {
                    updateHours.add(
                            new CourseHour() {
                                {
                                    setId(hour.getId());
                                    setDuration(hour.getDuration());
                                }
                            });
                }
            }
        }
    }

}
